//
//  KSkipConfigViewController.m
//
//
//  Created by lyh on 2018/12/29.
//  Copyright © 2018年 bwx. All rights reserved.
//

#import "KSkipConfigViewController.h"

#define kGGGScreenWidth    ([UIScreen mainScreen].bounds.size.width)
#define kGGGScreenHeight    ([UIScreen mainScreen].bounds.size.height)

@interface KSkipConfigViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIActivityIndicatorView *spinner;
@property (nonatomic, strong) UIButton *btnHome;
@property (nonatomic, strong) UIButton *btnBack;
@property (nonatomic, strong) UIButton *btnForward;
@property (nonatomic, strong) UIButton *btnReload;
@property (nonatomic, strong) UIButton *btnStop;
@end

@implementation KSkipConfigViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kGGGScreenWidth, kGGGScreenHeight-40)];
    _webView.backgroundColor = [UIColor whiteColor];
    _webView.delegate = self;
    _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _webView.scalesPageToFit = YES;
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.hzUrl]]];
    [self.view addSubview:_webView];
    
//    _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//    _spinner.bounds = CGRectMake(0, 0, 40, 40);
//    _spinner.center = self.webView.center;
//    _spinner.hidesWhenStopped = YES;
//    [self.view addSubview:_spinner];
//    [_spinner startAnimating];
    
    
    UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, kGGGScreenHeight-40, kGGGScreenWidth, 40)];
    bar.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:bar];
    
    CGFloat w = 60;
    CGFloat h = bar.frame.size.height;
    CGFloat offset = (bar.frame.size.width-w*5)/6;
    
    UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom];
    btnHome.frame = CGRectMake(offset, 0, w, h);
//    [btnHome setTitle:@"首页" forState:UIControlStateNormal];
    [btnHome setImage:[UIImage imageNamed:@"web0_home"] forState:UIControlStateNormal];
    [btnHome addTarget:self action:@selector(home) forControlEvents:UIControlEventTouchUpInside];
    [bar addSubview:btnHome];
    self.btnHome = btnHome;
    
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBack.frame = CGRectMake(CGRectGetMaxX(btnHome.frame) + offset, 0, w, h);
//    [btnBack setTitle:@"向后" forState:UIControlStateNormal];
    [btnBack setImage:[UIImage imageNamed:@"web0_back"] forState:UIControlStateNormal];
    [btnBack setImage:[UIImage imageNamed:@"web0_back_disable"] forState:UIControlStateDisabled];
    [btnBack addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    [bar addSubview:btnBack];
    self.btnBack = btnBack;
    
    UIButton *btnForward = [UIButton buttonWithType:UIButtonTypeCustom];
    btnForward.frame = CGRectMake(CGRectGetMaxX(btnBack.frame) + offset, 0, w, h);
//    [btnForward setTitle:@"向前" forState:UIControlStateNormal];
    [btnForward setImage:[UIImage imageNamed:@"web0_next"] forState:UIControlStateNormal];
    [btnForward setImage:[UIImage imageNamed:@"web0_next_disable"] forState:UIControlStateDisabled];
    [btnForward addTarget:self action:@selector(goForward) forControlEvents:UIControlEventTouchUpInside];
    [bar addSubview:btnForward];
    self.btnForward = btnForward;
    
    UIButton *btnReload = [UIButton buttonWithType:UIButtonTypeCustom];
    btnReload.frame = CGRectMake(CGRectGetMaxX(btnForward.frame) + offset, 0, w, h);
//    [btnReload setTitle:@"刷新" forState:UIControlStateNormal];
    [btnReload setImage:[UIImage imageNamed:@"web0_reload"] forState:UIControlStateNormal];
    [btnReload addTarget:self action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
    [bar addSubview:btnReload];
    self.btnReload = btnReload;
    
    UIButton *btnStop = [UIButton buttonWithType:UIButtonTypeCustom];
    btnStop.frame = CGRectMake(CGRectGetMaxX(btnReload.frame) + offset, 0, w, h);
//    [btnStop setTitle:@"停止" forState:UIControlStateNormal];
    [btnStop setImage:[UIImage imageNamed:@"web0_close"] forState:UIControlStateNormal];
    [btnStop addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
    [bar addSubview:btnStop];
    self.btnStop = btnStop;
}

-(void)home {
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.hzUrl]]];
}

-(void)goBack {
    if (_webView.canGoBack) {
        [_webView goBack];
    }
    [self reflashButtonState];
}

-(void)goForward {
    if (_webView.canGoForward) {
        [_webView goForward];
    }
    [self reflashButtonState];
}

-(void)reload {
    [_webView reload];
}

-(void)stop {
    [_webView stopLoading];
}


- (void)reflashButtonState
{
    if (_webView.canGoBack) {
        _btnBack.enabled = YES;
    } else {
        _btnBack.enabled = NO;
    }
    
    if (_webView.canGoForward) {
        _btnForward.enabled = YES;
    } else {
        _btnForward.enabled = NO;
    }
}
  

#pragma mark - webView delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
//    [self.spinner startAnimating];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    [self reflashButtonState];
    return YES;
}


#pragma mark - web 加载成功
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self reflashButtonState];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
//    [self.spinner stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [self reflashButtonState];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
//    [self.spinner stopAnimating];
}


@end
